package com.itheima.controller;

import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.itheima.domain.User;
import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/ex")
public class UserController {
    @Autowired
    private AccountDao accountDao;
    @RequestMapping("/test")
    @ResponseBody
    @Transactional
    public List<User> save(@RequestBody Account account) {
        System.out.println("user controller save is running ...");

        //模拟业务层发起调用产生了异常
//        int i = 1/0;
//        String str = null;
//        str.length();
        accountDao.save(account);
        //对用户的非法操作进行判定，并包装成异常对象进行处理，便于统一管理
//        if(account.getName().trim().length() < 8){
////            throw new BusinessException("对不起，用户名长度不满足要求，请重新输入！");
////        }
////        if(account.getMoney() < 0){
////            throw new BusinessException("对不起，money必须是0到100之间的数字！");
////        }



        User u1 = new User();
        u1.setName("Tom");
        u1.setAge(3);
        User u2 = new User();
        u2.setName("Jerry");
        u2.setAge(5);
        ArrayList<User> al = new ArrayList<User>();
        al.add(u1);
        al.add(u2);

        return al;
    }
}
